package com.ssm.dao;

import com.ssm.domain.Role;
import com.ssm.domain.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDao {
    @Select("select * from users where username = #{u} ")
    @Results({
            @Result(property = "id", id = true, column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "email", column = "email"),
            @Result(property = "password", column = "password"),
            @Result(property = "phoneNum", column = "phoneNum"),
            @Result(property = "status", column = "status"),
            @Result(property = "roles", column = "id", many = @Many(select = "com.ssm.dao.RoleDao.findByUserId"))
    })
    UserInfo findByUsername(String username);

    @Select("select * from users")
    @Results({
            @Result(property = "id", id = true, column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "email", column = "email"),
            @Result(property = "password", column = "password"),
            @Result(property = "phoneNum", column = "phoneNum"),
            @Result(property = "status", column = "status"),
            @Result(property = "roles", column = "id", many = @Many(select = "com.ssm.dao.RoleDao.findByUserId"))
    })
    List<UserInfo> findAll();

    @Insert("insert into users(email,username,password,phoneNum,status) values(#{email},#{username},#{password},#{phoneNum},#{status})")
    void save(UserInfo userInfo);

    @Select("select * from users where id = #{id}")
    @Results({
            @Result(property = "id", id = true, column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "email", column = "email"),
            @Result(property = "password", column = "password"),
            @Result(property = "phoneNum", column = "phoneNum"),
            @Result(property = "status", column = "status"),
            @Result(property = "roles", column = "id", many = @Many(select = "com.ssm.dao.RoleDao.findByUserId"))
    })
    UserInfo findById(String id);

    @Select("select  * from role  where id not in (select roleId from users_role where userId=#{i})")
    List<Role> findOtherRoles(String id);

    @Insert("insert into USERS_ROLE(userid,roleid) values(#{param1},#{param2})")
    void addRoleToUser(String id, String s);
}
